#!/usr/bin/env python
# -*- coding:utf-8 -*-

#Import urllib for HTML page fetching
import urllib2
#Import regular expression lib for Temp. Data extracting
import re

#Weather data source link
url = r'http://www.google.com.hk/m/search?site=weather&gl=cn&hl=zh-CN&q=%E6%B7%B1%E5%9C%B3'

#Fetch whole HTML Page to String data
try:
    data = urllib2.urlopen(url).read()
#While URL is illegal
except ValueError:
    #Print "Bad link. Try to add 'http://' in the front."
    print("Bad link. Try to add 'http://' in the front.")
    #Quit the program
    exit()
#While BadStatusLine
except httplib.BadStatusLine:
    #Print "Oops! Error ocurs while connecting."
    print("Oops! Error ocurs while connecting.")
    #Quit the program
    exit()

#Search certain pattern and extract Temp. Data
matobj = re.search("&nbsp;(.*)&nbsp;<b>([-]*\d+ \/ [-]*\d+).*&nbsp;<\/b>.*&nbsp;(.*)&nbsp;([-]*\d+ \/ [-]*\d+).*&nbsp;.*&nbsp;(.*)&nbsp;([-]*\d+ \/ [-]*\d+).*&nbsp;", data)

#If matching not failed
if  None != matobj:
    #Combine extracted Temp. Data
    resultString = matobj.group(1)+matobj.group(2)+'\n'+matobj.group(3)+matobj.group(4)+'\n'+matobj.group(5)+matobj.group(6)
#Else
else:
    #Print nothing matched
    print 'nothing matched'
#Endif

print (resultString)
#Print resultString as GB2312
print (resultString.decode('utf-8').encode('gb2312'))



